//
//  TextFieldFormControl.m
//  iCodeMac
//
//  Created by Andrew Barry on 29/03/09.
//  Copyright 2009 Barry Computer Services. All rights reserved.
//

#import "TextFieldFormControl.h"
#import "JavascriptForm.h"

@implementation TextFieldFormControl
-(id) valueForProperty: (NSString *) name {
	if ([name isEqualToString:@"text"]) {
		return [(NSTextField *) view stringValue];
	}
	if ([name isEqualToString:@"password"]) {
		return [NSNumber numberWithBool:password];
	}
	if ([name isEqualToString:@"editable"]) {
		return [NSNumber numberWithBool:editable];
	}
	if ([name isEqualToString:@"selectable"]) {
		return [NSNumber numberWithBool:selectable];
	}
	if ([name isEqualToString:@"dataSource"]) {
		return dataSource;
	}
	if ([name isEqualToString:@"dataField"]) {
		return dataField;
	}
	return [super valueForProperty: name];
}

-(void) setValue: (id) val forProperty:(NSString*) name {
	if ([name isEqualToString:@"text"]) {
		[(NSTextField *) view setStringValue: (NSString *) val];
	} else if ([name isEqualToString:@"password"]) {
		password = [val boolValue];
	} else if ([name isEqualToString:@"editable"]) {
		editable = [val boolValue];
	} else if ([name isEqualToString:@"selectable"]) {
		selectable = [val boolValue];
	} else if ([name isEqualToString:@"dataSource"]) {
		dataSource = val;
	} else if ([name isEqualToString:@"dataField"]) {
		[dataField autorelease];
		dataField = [val retain];
	} else {
		[super setValue: val forProperty: name];
	}
}

-(NSArray *) getEvents {
	return [NSArray arrayWithObjects:@"Change", nil];
}

-(void) saveFields: (NSMutableDictionary *) content {
	[content setObject: [(NSTextField *) view stringValue] forKey: @"text"];
	[self saveField: content forKey: @"password"];
	[self saveField: content forKey: @"selectable"];
	[self saveField: content forKey: @"editable"];
	if (dataSource != nil && dataSource != [NSNull null]) {
		[content setObject: [dataSource controlName] forKey: @"dataSource"];
	}
	if (dataField)
		[self saveField: content forKey: @"dataField"];
	[super saveFields: content];
}

-(NSView *) load: (NSDictionary *) defn withFrame: (NSRect) frame {
	NSString *text = [defn objectForKey:@"text"];
	if (!text)
		text = controlType;
	password = [[defn objectForKey: @"password"] boolValue];
	if ([defn objectForKey: @"selectable"])
		selectable = [[defn objectForKey: @"selectable"] boolValue];
	else
		selectable = ![controlType isEqualToString:@"label"];

	if ([defn objectForKey: @"selectable"])
		editable = [[defn objectForKey: @"editable"] boolValue];
	else
		editable = ![controlType isEqualToString:@"label"];
	
	dataSourceLoadName = [defn objectForKey:@"dataSource"];
	dataField = [defn objectForKey:@"dataField"];
	
	if ([controlType isEqualToString:@"label"]) {
		NSTextField *lbl = [[NSTextField alloc] initWithFrame:frame];
		[lbl setStringValue:text];
		[lbl setEditable:NO];
		[lbl setBezeled:NO];
		[lbl setDrawsBackground:NO];
		return lbl;
	} else {
		NSTextField *lbl = [[NSTextField alloc] initWithFrame:frame];
		[lbl setStringValue:text];
		[lbl setEditable:NO];
		return lbl;
	}
}

-(void) postLoad {
	FormEditor *editor = [formDocument editor];
	if ([dataSourceLoadName length] > 0) {
		dataSource = [editor controlByName:dataSourceLoadName];
	}
	
	[super postLoad];
}

-(NSString *) propsNibName {
	return @"TextfieldProps";
}

-(NSArray *) autocompletes {
	return [NSArray arrayWithObjects: @"left", @"top", @"width", @"height", @"visible", @"text", @"enabled", nil];
}


@end
